package pub.guoxin.oops.manager.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pub.guoxin.oops.manager.handler.AlarmFieldNameConstants;
import pub.guoxin.oops.manager.service.DataAlarmConfigService;
import pub.guoxin.oops.model.Boundary;
import pub.guoxin.oops.model.DataAlarmConfig;
import pub.guoxin.oops.repository.DataAlarmConfigRepository;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Create by guoxin on 2018/4/15
 */
@Service
public class DataAlarmConfigServiceImpl implements DataAlarmConfigService {

    @Autowired
    private DataAlarmConfigRepository dataAlarmConfigRepository;

    @Override
    public List<DataAlarmConfig> modify(List<DataAlarmConfig> configs) {
        return dataAlarmConfigRepository.save(configs);
    }

    @Override
    public void init(String companyId) {
        Set<String> fieldNames = AlarmFieldNameConstants.FIELD_NAME_DEFAULT_VALUE.keySet();
        List<DataAlarmConfig> collect = new ArrayList<>();
        for (String fieldName : fieldNames) {
            Boundary boundary = AlarmFieldNameConstants.FIELD_NAME_DEFAULT_VALUE.get(fieldName);
            DataAlarmConfig dataAlarmConfig = new DataAlarmConfig(companyId, fieldName, boundary);
            collect.add(dataAlarmConfig);
        }
        dataAlarmConfigRepository.insert(collect);
    }

    @Override
    public List<DataAlarmConfig> all(String companyId) {
        return dataAlarmConfigRepository.findByCompanyId(companyId);
    }
}
